const classModel   = require('./classModel/classModel');
const userModel    = require('./userModel/userModel');
const stuModel     = require('./studentModel/studentModel');
const classStudent = require('./class_studentModel/class_studentModel');
const numberModel  = require('./numberModel/numberModel');
const chapterModel = require('./chapterModel/chapterModel');
const courseModel  = require('./courseModel/courseModel');
const seriesModel  = require('./seriesModel/seriesModel');
const notifyModel  = require('./notifyModel/notifyModel');
const workPubModel = require('./worksModel/works_public');
const workTeaModel = require('./worksModel/works_teach');
const openedModel  = require('./openedModel/openedModel');
const orgInfoModel = require('./org_infoModel');
const orgWorks     = require('./org_worksModel');
const wechatModel  = require('./wechatModel');
const chapter_opened = require('./chapter_opened');

const belongsTo = (model, key) => {
  model[0].getModel().belongsTo(model[1].getModel(),{ foreignKey: key[0], targetKey: key[1]})
};
const hasOne = (model, key) => {
  model[0].getModel().hasOne(model[1].getModel(),{ foreignKey: key[0], targetKey: key[1]})
};

const belongsToMany = (model, key) => {
  model[0].getModel().belongsToMany(model[2].getModel(),{through: model[1].getModel(), foreignKey: key[0]});
  model[2].getModel().belongsToMany(model[0].getModel(),{through: model[1].getModel(), foreignKey: key[1]});
};

const config = [
  {mode: 'hasOne', model: [userModel, chapterModel], key: ['org','org'], both: true},
  {mode: 'hasOne', model: [userModel, courseModel], key: ['org', 'org']},
  {mode: 'hasOne', model: [stuModel, wechatModel], key: ['id', 'id']},
  // {mode: 'belongsTo', model: [userModel, orgInfoModel], key: ['org', 'id'], both: true},
  {mode: 'hasOne', model: [orgInfoModel, userModel], key: ['org', 'id']},
  {mode: 'hasOne', model: [stuModel, orgInfoModel], key: ['id', 'org']},
  {mode: 'hasOne', model: [userModel, seriesModel], key: ['org', 'org'], both: true},
  {mode: 'hasOne', model: [stuModel, seriesModel], key: ['org', 'org'], both: true},
  {mode: 'hasOne', model: [chapterModel, chapter_opened], key: ['chapter_id', 'id']}, // 暂定
  {mode: 'belongsTo', model: [openedModel, workTeaModel], key: ['work_id', 'id']},
  {mode: 'belongsTo', model: [courseModel, workTeaModel], key: ['work_id', 'id']},
  {mode: 'belongsTo', model: [userModel, orgInfoModel], key: ['org', 'id']},
  {mode: 'belongsTo', model: [classModel, userModel], key: ['org','org']},
  {mode: 'belongsToMany', model: [classModel, classStudent, stuModel], key: ['class_id','stu_id']},
  {mode: 'belongsToMany', model: [stuModel, openedModel, workTeaModel], key: ['stu_id', 'work_id']},
  {mode: 'belongsToMany', model: [userModel, orgWorks, workTeaModel], key: ['org', 'work_id']}
];

function setModelRelation() {
  for (const item of config) {
    if ((item.mode === 'belongsTo' || item.mode === 'hasOne') && item.both) {
      belongsTo(item.model, item.key);
      item.model.reverse();
      item.key.reverse();
      hasOne(item.model, item.key);
    }
    else if (item.mode === 'belongsTo') belongsTo(item.model, item.key);
    else if (item.mode === 'hasOne') hasOne(item.model, item.key);
    else if (item.mode === 'belongsToMany') belongsToMany(item.model, item.key);
  }
}

setModelRelation();

module.exports = {
  classModel,
  userModel,
  stuModel,
  numberModel,
  classStudent,
  chapterModel,
  courseModel,
  seriesModel,
  notifyModel,
  workPubModel,
  workTeaModel,
  openedModel,
  orgWorks,
  chapter_opened,
  wechatModel,
  orgInfoModel
};
// const attr = ['id','name','org','fileURL'];
// module.exports = stuModel.findByRelation(['id'],{id:1, org: 1}, {
//   model: works_teachModel.getModel(),
//   attributes: attr
// }).then(function (result) {
//   console.log(getResultToObject(result, 'orgworks', attr))
//
// })

// module.exports =
//
//     openedModel.createBatch([{
//       stu_id:1,
//       course_id:4,
//       work_id: 1,
//       createdAt: Date.now()
//     }, {
//       stu_id:22,
//       course_id:6,
//       work_id: 3,
//       createdAt: Date.now()
//     }],{ updateOnDuplicate: ['stu_id','work_id'] }).then(function () {
//       console.log('ok')
//     })

    // 查找已开通的课程
    // openedModel.findByFilter([
    //   'stu_id',
    //   'course_id'
    // ],{
    //   [Op.or] : [{
    //     stu_id:1,
    //     course_id:4
    //   }, {
    //     stu_id:22,
    //     course_id:5
    //   }]
    // }).then(function (openList) {
    //   // TODO 包含重复数据
    //   if (openList) {
    //
    //   }
    //   console.log(openList)
    // })


// const Sequelize = require('sequelize');
//
// module.exports =  stuModel.increment('learn_number',{id:1}).then(function (result) {
//   console.log(result)
// });


